package com.china08.yxyapi.api.manage.edu.platform.statistics.weikepandect;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.service.yxyresource.statistics.ResourceWeikeAllCountService;

import io.swagger.annotations.ApiOperation;

/**
 * 教育局平台办公  微课统计     微课总量   微课使用数   最近新增微课
 * 
 */
@CrossOrigin
@RestController
public class WeikeCountAllController extends YxyApiBaseController{
	@Autowired
	public ResourceWeikeAllCountService resourceWeikeAllCountService;
	@Autowired
	public YxyResResourceRepository yxyResResourceRepository;
	
	
	@ApiOperation(value = "微课统计     微课总量   微课使用数   最近新增微课")
	@ResponseBody
	@RequestMapping(value = "/api_yw/manage/edu/platform/statistics/weikepandect/weikecount2/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public RespModel precess(HttpServletRequest request, HttpServletResponse response) {
		RespModel model = new RespModel();
        Integer schoolBureauLevel =getProfile(request, response).getSchoolBureauLevel();
        String districtCode=null;
        List<YxyResResource> yxyResResourceLst=new ArrayList<YxyResResource>();
        if (schoolBureauLevel==0) {
        	districtCode=getProfile(request, response).getSchoolAreaIdDistrict();
        	yxyResResourceLst=yxyResResourceRepository.findByTypeIdAndDistrictCode("yx14",districtCode);
		}else if(schoolBureauLevel==1){
			districtCode=getProfile(request, response).getSchoolAreaIdCity();
			yxyResResourceLst=yxyResResourceRepository.findByTypeIdAndCityCode("yx14",districtCode);
		}else if(schoolBureauLevel==2){
			districtCode=getProfile(request, response).getSchoolAreaIdProvince();
			yxyResResourceLst=yxyResResourceRepository.findByTypeIdAndProvinceCode("yx14",districtCode);
		}
        StatisticsResourceAllCountModel statisticsResourceAllCountModel=new StatisticsResourceAllCountModel();
        //调用此方法查询资源数据总览数据
        List<Long> countLst= resourceWeikeAllCountService.statisticsResourceCount(districtCode,schoolBureauLevel);
        
        //资源总数量
        statisticsResourceAllCountModel.resourceCount = countLst.get(0);
        //最近新增资源
        statisticsResourceAllCountModel.nearResourceCount = countLst.get(1);
        // 微课使用数
        Long areadyResourceCount=0l;
        for (YxyResResource yxyResResource : yxyResResourceLst) {
        	areadyResourceCount+=yxyResResource.getPv();
		}
        statisticsResourceAllCountModel.areadyResourceCount=areadyResourceCount;
        
        
        model.statisticsResourceAllCountModel=statisticsResourceAllCountModel;
        return model;
    }
	 protected static class RespModel extends YxyApiRespModel {
	    //统计资源统计模型
		public StatisticsResourceAllCountModel statisticsResourceAllCountModel;
	 }
}
/**
 * 
 */
class StatisticsResourceAllCountModel {
	//资源总数量
	public Long resourceCount;
	//资源使用数
    public Long areadyResourceCount;
	//最近新增资源
	public Long nearResourceCount;
}